package ltu.m7011e.client.view;

import java.util.List;

import ltu.m7011e.client.presenter.PostLinkPresenter;
import ltu.m7011e.shared.SharedConstants;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;


public class PostLinkView extends DialogBox implements PostLinkPresenter.Display {

	/**
	 * An instance of the  Constants.
	 */
	private final SharedConstants constants = GWT.create(SharedConstants.class);

	private final DialogBox postBox ;
	private final Button closePostButton;
	private final Button postLinkButton;
	private TextBox urlField ;
	private TextBox descriptionField;
	private HTML serverPostResponseLabel = new HTML();;
	private VerticalPanel dialogVPostPanel;
	private HorizontalPanel ratioHPanel;
	

	public void setData(List<String> data) {
		// Fill the view with data - unused ?
	}

	public PostLinkView() {
		GWT.log("PostLinkView - Contructor Called");
		//DecoratorPanel contentTableDecorator = new DecoratorPanel();
		//initWidget(contentTableDecorator);
		//contentTableDecorator.setWidth("100%");
		//contentTableDecorator.setWidth("18em");

		// Create the popup POST box
		postBox = new DialogBox();
		//initWidget(postBox);

		// Initialize Button and field
		postBox.setText("Post a new link !");
		postBox.setAnimationEnabled(true);
		postBox.setGlassEnabled(true);
		closePostButton = new Button("Close");
		postLinkButton = new Button("Submit !");
		urlField = new TextBox();
		urlField.setText("www.youtube.com");
		urlField.setWidth("20em");
		descriptionField = new TextBox();
		descriptionField.setWidth("20em");
		descriptionField.setText("Miaouuuuuu !");

		// We can set the id of a widget by accessing its Element
		closePostButton.getElement().setId("closePostButton");
		// TODO Change the VertcialPanel to a better container for the POST operation
		dialogVPostPanel = new VerticalPanel();
		dialogVPostPanel.addStyleName("dialogVPostPanel");
		dialogVPostPanel.add(new HTML("<br>" + constants.cPostLinkEnterUrl()));
		dialogVPostPanel.add(urlField);

		// Implementation of the ratio panel for categories
		// Header with Categories selection Label
		dialogVPostPanel.add(new HTML("<br>" + constants.cPostLinkRadioSelectCategory()));
		ratioHPanel = generateRadioPanel();
		ratioHPanel.setSpacing(5);
		dialogVPostPanel.add(ratioHPanel);

		dialogVPostPanel.add(new HTML("<br>" + constants.cPostLinkEnterDescription()));
		dialogVPostPanel.add(descriptionField);

		dialogVPostPanel.add(new HTML("<br>"));
		dialogVPostPanel.add(serverPostResponseLabel);
		dialogVPostPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		HorizontalPanel buttonHPanel = new HorizontalPanel();
		buttonHPanel.setSpacing(10);
		buttonHPanel.add(postLinkButton);
		buttonHPanel.add(closePostButton);
		dialogVPostPanel.add(buttonHPanel);
		postBox.setWidget(dialogVPostPanel);
		postBox.setAutoHideOnHistoryEventsEnabled(true);

		postBox.center();
		//contentTableDecorator.add(postBox);
	}

	public HasClickHandlers getPostLinkButton() {
		return postLinkButton;
	}

	public HasClickHandlers getClosePostButton() {
		return closePostButton;
	}

	public HasValue<String> getUrlField() {
		return urlField;
	}

	public HasValue<String> getDescriptionField() {
		return descriptionField;
	}
	
	public DialogBox getPostDialogBox(){
		return postBox;
	}

	public String getLinkCat() {
		String linkCategory="default";
		// We recover the category by analyzing widget of the ratioHPanel
		for (Widget widget : ratioHPanel) {
			// TODO : need to check through instanceof ?
			if ( ((RadioButton) widget).getValue()){
				// If the radio button is selected we attribute the category name 
				return linkCategory = ((RadioButton) widget).getHTML();
			}
		}
		return linkCategory;
	}


	/**
	 * Generate the ratio button for each categories of link and insert them into a HorizontalPanel
	 * (The categories are loaded from the <code>enum Categories</code> in share.tools
	 * @return
	 */
	private HorizontalPanel generateRadioPanel(){

		int i=0;
		final HorizontalPanel radioPanel = new HorizontalPanel();
		String[] availaibleCats = constants.cLinkAvailableCategories();
		for (String category : availaibleCats) {
			if ( i== 0){
				RadioButton firstRadioButton = new RadioButton("Categories", category);
				firstRadioButton.setValue(true);
				i++;
			}
			radioPanel.add(new RadioButton("Categories", category));
		}
		return radioPanel;
	}

	public Widget asWidget() {
		return this;
	}
}
